home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
tex
/
sed15.zip
/
SEDCOMP.C
< prev
next >
Wrap
Text File
|
1991-09-28
|
28KB
|
670 lines
/*
* sedcomp.c -- stream editor main and compilation phase
*
* The stream editor compiles its command input (from files or -e options) into
* an internal form using compile() then executes the compiled form using
* execute(). Main() just initializes data structures, interprets command
* line options, and calls compile() and execute() in appropriate sequence.
* The data structure produced by compile() is an array of compiled-command
* structures (type sedcmd). These contain several pointers into pool[], the
* regular-expression and text-data pool, plus a command code and g & p
* flags. In the special case that the command is a label the struct will
* hold a ptr into the labels array labels[] during most of the compile,
* until resolve() resolves references at the end. The operation of execute()
* is described in its source module.
*
* ==== Written for the GNU operating system by Eric S. Raymond ====
* v1.1, 19 Jun 91
* Toad Hall Tweak
* - Mostly minor tweaks to make it compile with Borland's TC v2.0.
* - No more feelthy debug.
* - We're prototyping now.
* See VERSION.NOT for details.
*
* modified Sep 91 by Howard Helman (h**2) for BC++ and Sun4 plus fixes:
* (those marked **** were critical)
* 1. l command cleanup (indexing and quoting)
* 2. first line problem (should have been delete FALSE) ****
* 3. y command compile funny BC++ problem with chars and ints
* 4. fixed `\' escapes in patterns
* 5. fixed `\' escapes in rhs
* 6. fixed `\' escapes in y strings
* 7. fixed `\' escapes in inserted text
* 8. fixed `\' in sets (all fixed by fixquote routine)
* 9. RE bad looping on error message *****
* 10. reworked entire selected routine
* 11. spaces after -e -f and nothing after -g -n
* 12. errors to stderr and general error message fixups
* 13. usage message when no args
* 14. Make it compile under Sun Unix with minimum lint complaints
* 15. Make it compile under BC++ 2.0 *****
* 16. Fix recognition of last line to edit
* 17. ; # and initial space clean ups
* 18. No `\` escapes in file names or labels
* 19. Last line may not have \n in commands
* 20. 256 bit characters in all contexts
* 21. Add + option to second address
* 22. allow \{m,n\} RE's including after \1 as for *; + now \{1,\}
* 23. allow \< and \> RE's
* 24. Genbuf now extremly long to hold everything(was 71!!) *****
* 25. Misc cleanups for n, N, & D commands range checks cleaned up.
* 26. Reset inrange flag on exit from {} nesting
* 27. Blanks after : (actually all of label processing fixed up
* 28. - in character character sets now works for ranges
* 29. g flag in s command cleanup used ++ instead of = 1
* 30. made separate -e and -f input routines and fixed up gettext
* 31. RELIMIT replaced by poolend allows REs to be of any size
* 32. \0 character is now an error in an RE body
* 33. address of 000 now illegal
* 34. trailing arguments of s command handled properly
* 35. & substitutions fixed(previously could not escape)
* 36. handling of lastre
* 37. % as repeat last rhs added
* 38. nth substitution only added to s command
* 39. \?RE? in addresses added
* 40. No range on { command
v1.4, Toad Hall, 20 Sep 91
*/
#ifdef OTHER
#include "compiler.h"
#include "debug.h"
#endif
#ifdef LATTICE
#define void int
#endif
#include <stdio.h> /* uses getc, fprintf, fopen, fclose */
#include "sed.h" /* command type struct and name defines */
/***** public stuff ******/
#define MAXCMDS 200 /* maximum number of compiled commands */
#define MAXLINES 256 /* max # numeric addresses to compile */
/* main data areas */
char linebuf[MAXBUF + 1]; /* current-line buffer */
sedcmd cmds[MAXCMDS + 1]; /* hold compiled commands */
long linenum[MAXLINES]; /* numeric-addresses table */
/* miscellaneous shared variables */
int nflag; /* -n option flag */
static int gflag; /* -g option flag */
int eargc; /* scratch copy of argument count */
sedcmd *pending = NULL; /* next command to be executed */
char bits[] = {1, 2, 4, 8, 16, 32, 64, 128};
/***** module common stuff *****/
#define POOLSIZE 10000 /* size of string-pool space */
#define WFILES 10 /* max # w output files that can be compiled */
#define MAXDEPTH 20 /* maximum {}-nesting level */
#define MAXLABS 50 /* max # of labels that can be handled */
#define SKIPWS(pc) while((*pc==' ')||(*pc=='\t')||*pc=='\f'||*pc=='\v')pc++
#define ABORT(msg) (fprintf(stderr, msg, linebuf), exit(2))
#define IFEQ(x, v) if (*x == v) x++ , /* do expression */
/* error messages */
static char BADGCNT[]="sed: bad value for match count on s command %s\n";
static char AGMSG[] = "sed: garbled address %s\n";
static char CGMSG[] = "sed: garbled command %s\n";
static char TMTXT[] = "sed: too much text: %s\n";
static char AD1NG[] = "sed: no addresses allowed for %s\n";
static char AD2NG[] = "sed: only one address allowed for %s\n";
static char TMCDS[] = "sed: too many commands, last was %s\n";
static char COCFI[] = "sed: cannot open command-file %s\n";
static char UFLAG[] = "sed: unknown flag %c\n";
static char CCOFI[] = "sed: cannot create %s\n";
static char ULABL[] = "sed: undefined label %s\n";
static char TMLBR[] = "sed: too many {'s %s\n";
static char NSCAX[] = "sed: no such command as %s\n";
static char TMRBR[] = "sed: too many }'s %s\n";
static char DLABL[] = "sed: duplicate label %s\n";
static char TMLAB[] = "sed: too many labels: %s\n";
static char TMWFI[] = "sed: too many w files %s \n";
static char REITL[] = "sed: RE too long: %s\n";
static char TMLNR[] = "sed: too many line numbers %s\n";
static char TRAIL[] = "sed: command \"%s\" has trailing garbage\n";
static char USAGE[] = "usage: sed [-n] [-g] [-e cmds] [-f cmdfile] files\n";
static char NEEDB[] = "sed: error proccessing: %s\n"; /*hh 12*/
static char NOARG[] = "sed: no argument for -e\n"; /*hh 12*/
static char ILFQT[] = "sed: bad expression %4.4s\n"; /*hh 12*/
static char BADRANGE[]= "sed: range error in set %s\n";
typedef struct { /* represent a command label */
char *name; /* the label name */
sedcmd *list; /* it's on the label search list */
sedcmd *address; /* pointer to the cmd it labels */
} label;
/* label handling */
static label labels[MAXLABS]; /* here's the label table */
static label *curlab = labels + 1; /* pointer to current label */
/* string pool for regular expressions, append text, etc. etc. */
static char pool[POOLSIZE]; /* the pool */
static char *fp = pool; /* current pool pointer */
static char *poolend = pool + POOLSIZE; /* pointer past pool end */
/* compilation state */
static FILE *cmdf = NULL; /* current command source */
static char *cp = linebuf; /* compile pointer */
static sedcmd *cmdp = cmds; /* current compiled-cmd ptr */
static char *lastre = NULL; /* old RE pointer */
static char *lastrhs= NULL; /* old RHS pointer*/
static int bdepth = 0; /* current {}-nesting level */
static int bcount = 0; /* # tagged patterns in current RE */
static char **eargv; /* scratch copy of argument list */
/* imported functions */
#ifdef __TURBOC__ /* v1.1 */
#include <string.h>
#include <stdlib.h> /* exit() */
#include <ctype.h>
#define Void void /*K&R and Std C compatibility*/
static void compile(int eflag),einit(void);
static void resolve(void);
extern void execute(char *file);/* execute compiled command (in SEDEXEC.C) */
static char fixquote(char**);
static int ecmdline(void), fcmdline(void);
static int address(char **expbuf,int pass);
static int cmdcomp(register char cchar);
static char *gettext(register char *txp,int doq);
static label *search(void);
static int recomp(char **expbuf, char redelim);
static int rhscomp(char **rhsp, char delim);
static int ycomp(void);
static char tox(char);
static int processm(void);
#else /* !__TURBOC__*/
#define Void
extern int strcmp()